package top.xcxdl.rural.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import top.xcxdl.rural.common.Res;
import top.xcxdl.rural.entity.RuralExamination;
import top.xcxdl.rural.entity.RuralQuestion;
import top.xcxdl.rural.service.IRuralExamQuestionService;
import top.xcxdl.rural.service.IRuralExaminationService;
import top.xcxdl.rural.service.IRuralQuestionService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/admin/examination")
public class BackRuralExaminationController {

    @Resource
    private IRuralExaminationService service;

    @Resource
    private IRuralExamQuestionService examQuestionService;

    @Resource
    private IRuralQuestionService questionService;


    @GetMapping("/getList")
    @ApiOperation("获取题库列表")
    @ResponseBody
    public Res<Object> getList(
            @RequestParam(name = "page_no") Integer pageNo,
            @RequestParam(name = "page_size") Integer pageSize,
            @RequestParam(name = "title", required = false) String key
    ) {
        PageInfo<RuralExamination> pageInfo = service.getList(pageNo, pageSize, key);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return Res.success(jsonObject);
    }

    /**
     * 根据作业id获取题目列表
     *
     * @param examId 作业id
     * @return 题目列表
     */
    @GetMapping("/getListById")
    @ApiOperation("获取题库列表")
    @ResponseBody
    public Res<Object> getListById(
            @RequestParam(name = "exam_id") Integer examId
    ) {
        //获取题目-作业联表的题目ids
        List<Integer> ids = examQuestionService.getIdsByExamId(examId);
        //根据题目ids获取题目列表
        List<RuralQuestion> list = questionService.getListByIds(ids);
        return Res.success(list);
    }

    @PostMapping("/doEdit")
    @ApiOperation("新增 or 编辑题库")
    @ResponseBody
    public Res<Object> doEdit(
            @RequestBody JSONObject jsonObject
    ) {
        //获取exam_id
        String examIdTemp = String.valueOf(jsonObject.get("exam_id"));
        Integer examId = null;
        if (!"null".equals(examIdTemp) && !"".equals(examIdTemp)  && examIdTemp != null) {
            examId = Integer.valueOf(examIdTemp);
        }
        //获取题目ids
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        //获取作业名称
        String examName = String.valueOf(jsonObject.get("exam_name"));
        //获取总分
        Double totalScore = Double.valueOf(String.valueOf(jsonObject.get("total_score")));
        //获取及格分
        Double passScore = Double.valueOf(String.valueOf(jsonObject.get("pass_score")));

        //封装实体
        RuralExamination examination = new RuralExamination();
        examination.setExamId(examId);
        examination.setExamName(examName);
        examination.setExamTotalScore(BigDecimal.valueOf(totalScore));
        examination.setExamPassScore(BigDecimal.valueOf(passScore));
        int ret = 0;
        ret += service.doEdit(examination);
        if(ret > 0){
            //成功！无论新增还是修改，mybatisPlus都会为我们返回自增id
            ret += examQuestionService.doEdit(examination.getExamId(), ids);
        }
        return ret > 1?Res.success(examId == null?"添加成功":"编辑成功"): Res.error("操作失败");
    }

    @PostMapping("/doDelete")
    @ApiOperation("删除作业")
    @ResponseBody
    public Res<Object> doDelete(@RequestBody JSONObject jsonObject) {
        //获取id集合
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        //删除作业
        int ret = service.doDelete(ids);
        //然后删除作业-题目联表
        ret += examQuestionService.doDelete(ids);
        return ret > 0 ? Res.success("删除成功") : Res.error("删除失败");
    }
}
